<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
        /**
         * @param {TreeNode} root
         * @param {number} targetSum
         * @return {number[][]}
         */
        var pathSum = function (root, targetSum) {
            let res = [];

            let func = function (root, totalScore, arr) {
                if (!root) {
                    return;
                }
                totalScore = totalScore + root.val;
                arr.push(root.val);
                if (totalScore === targetSum) {
                    res.push([...arr]);
                }
                func(root.left, totalScore, arr);
                func(root.right, totalScore, arr);

            }
            func(root, 0, []);
            return res;
        };
        let root=[5,4,8,11,null,13,4,7,2,null,null,5,1];
        targetSum=22; 
        pathSum(root,targetSum);
    </script>
</body>

</html>